package st.business.tepth.business.user.busnumber.workholder.repair.work.content;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import st.business.tepth.R;
import st.business.tepth.business.user.busnumber.workholder.adapter.TroubleCommonAdapter;
import st.business.tepth.business.user.busnumber.workholder.adapter.TroublePagerAdapter;
import st.business.tepth.business.user.busnumber.workholder.adapter.TroubleProgramAdapter;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2019/4/22
 */
public class UiDelegate {
    private final TroublePagerAdapter mTroublePagerAdapter;
    private final TextView mTvSearchDiagnose;
    private final TextView mTvCommonDiagnose;
    private final RecyclerView mRvCommonDiagnose;
    private final TroubleCommonAdapter mCommonDiagnoseAdapter;
    private final TextView mTvProgram;
    private final RecyclerView mRvProgram;
    private final TroubleProgramAdapter mProgramAdapter;
    private final TextView mTvDone;
    private Context mContext;
    public STATE mSTATE;

    public enum STATE {
        CONFIRM_DIAGNOSE,
        CONFIRM_PROGRAM,
        WORK_DONE,
        WORK_CANCEL
    }

    public UiDelegate(
            Context context,
            TroublePagerAdapter troublePagerAdapter,
            TextView tvSearchDiagnose,
            TextView tvCommonDiagnose,
            RecyclerView rvCommonDiagnose,
            TroubleCommonAdapter commonDiagnoseAdapter,
            TextView tvProgram, RecyclerView rvProgram,
            TroubleProgramAdapter programAdapter, TextView tvDone) {
        mContext = context;
        mTroublePagerAdapter = troublePagerAdapter;
        mTvSearchDiagnose = tvSearchDiagnose;
        mTvCommonDiagnose = tvCommonDiagnose;
        mRvCommonDiagnose = rvCommonDiagnose;
        mCommonDiagnoseAdapter = commonDiagnoseAdapter;
        mTvProgram = tvProgram;
        mRvProgram = rvProgram;
        mProgramAdapter = programAdapter;
        mTvDone = tvDone;
    }

    public void init(int state) {
        if (state > 3) {
            workCanNotEdit();
        } else if (state == 3) {
            showCancelWork();
        } else {
            showCanEdit();
        }
    }

    private void showCanEdit() {
        mTvSearchDiagnose.setVisibility(View.VISIBLE);
        mTvCommonDiagnose.setVisibility(View.VISIBLE);
        mRvCommonDiagnose.setVisibility(View.VISIBLE);
    }

    private void showCancelWork() {
        mTvSearchDiagnose.setVisibility(View.GONE);
        mTvCommonDiagnose.setVisibility(View.GONE);
        mRvCommonDiagnose.setVisibility(View.GONE);
        mTvDone.setText("取消作业完成");
        mTvDone.setBackground(mContext.getResources()
                .getDrawable(R.drawable.selector_btn_operation));
        mTvDone.setTextColor(Color.parseColor("#333333"));
        mTvDone.setEnabled(true);
        mTvProgram.setVisibility(View.VISIBLE);
        mRvProgram.setVisibility(View.VISIBLE);
        mSTATE = STATE.WORK_CANCEL;
    }

    private void workCanNotEdit() {
        mTvSearchDiagnose.setVisibility(View.GONE);
        mTvCommonDiagnose.setVisibility(View.GONE);
        mRvCommonDiagnose.setVisibility(View.GONE);
        mTvDone.setText("作业已完成");
        mTvDone.setBackground(mContext.getResources()
                .getDrawable(R.drawable.shape_btn_operation_t));
        mTvDone.setTextColor(Color.parseColor("#ffffff"));
        mTvDone.setEnabled(false);
        mTvProgram.setVisibility(View.VISIBLE);
        mRvProgram.setVisibility(View.VISIBLE);
    }

    public void changeState() {
        if (mTroublePagerAdapter.getDiagnoses(false).isEmpty()) {
            //没有任何诊断
            mTvDone.setText("诊断未确认");
            mTvDone.setBackground(mContext.getResources()
                    .getDrawable(R.drawable.shape_btn_operation_t));
            mTvDone.setTextColor(Color.parseColor("#ffffff"));
            mTvDone.setEnabled(false);
        } else if (!mTroublePagerAdapter.getDiagnoses(true).isEmpty()) {
            //有临时诊断
            mTvDone.setText("确认诊断");
            mSTATE = STATE.CONFIRM_DIAGNOSE;
            mTvDone.setBackground(mContext.getResources()
                    .getDrawable(R.drawable.selector_btn_operation));
            mTvDone.setEnabled(true);
            mTvDone.setTextColor(Color.parseColor("#333333"));
        } else {
            //所有诊断已确认
            mTvDone.setText("诊断已确认");
            mTvDone.setBackground(mContext.getResources()
                    .getDrawable(R.drawable.shape_btn_operation_t));
            mTvDone.setTextColor(Color.parseColor("#ffffff"));
            mTvDone.setEnabled(false);
            //判断方案
            List<MultipleItemEntity> allProgram = mProgramAdapter.getData();
            if (allProgram.isEmpty()) {
                mTvProgram.setVisibility(View.GONE);
                mRvProgram.setVisibility(View.GONE);
            } else {
                //有方案
                mTvProgram.setVisibility(View.GONE);
                mRvProgram.setVisibility(View.GONE);
                boolean hasNoConfirm = false;
                for (MultipleItemEntity entity : allProgram) {
                    if (entity.getItemType() == TroubleProgramAdapter.
                            TYPE_RELEVANCE_UN_CONFIRM ||
                            entity.getItemType() == TroubleProgramAdapter.
                                    TYPE_SYSTEM_RECOMMEND_UN_CONFIRM ||
                            entity.getItemType() == TroubleProgramAdapter.
                                    TYPE_MANUAL_UN_CONFIRM) {
                        hasNoConfirm = true;
                    }
                    if (hasNoConfirm) {
                        //有未确认方案
                        mTvDone.setText("确认方案");
                        mSTATE = STATE.CONFIRM_PROGRAM;
                        mTvDone.setBackground(mContext.getResources()
                                .getDrawable(R.drawable.selector_btn_operation));
                        mTvDone.setEnabled(true);
                        mTvDone.setTextColor(Color.parseColor("#333333"));
                    } else {
                        //没有未确认方案
                        mTvDone.setText("作业完成");
                        mSTATE = STATE.WORK_DONE;
                        mTvDone.setBackground(mContext.getResources()
                                .getDrawable(R.drawable.selector_btn_operation));
                        mTvDone.setEnabled(true);
                        mTvDone.setTextColor(Color.parseColor("#333333"));
                    }
                }
            }
        }
        showCommon();
        showProgram();
    }

    public STATE getSTATE() {
        return mSTATE;
    }

    private void showCommon() {
        if (mCommonDiagnoseAdapter.getData().isEmpty()) {
            //没有推荐诊断
            mTvCommonDiagnose.setVisibility(View.GONE);
            mRvCommonDiagnose.setVisibility(View.GONE);
        } else {
            mTvCommonDiagnose.setVisibility(View.VISIBLE);
            mRvCommonDiagnose.setVisibility(View.VISIBLE);
        }
    }

    private void showProgram() {
        if (mProgramAdapter.getData().isEmpty()) {
            mTvProgram.setVisibility(View.GONE);
            mRvProgram.setVisibility(View.GONE);
        } else {
            mTvProgram.setVisibility(View.VISIBLE);
            mRvProgram.setVisibility(View.VISIBLE);
        }
    }
}
